/**
 * Created by djh on 2016/11/8.
 * 资金操作相关公共js
 *
 */
define("account",function(require,exports){
    var common=require("common");
    //var bankselector = require('bankSelector');
    //各个操作的url集合
    var urlJson = {
        allow:"/admin/fund/isSign",//是否允许资金操作
        virtual:"/admin/fund/getBalance",//附属账户余额
        moneyIn:"/admin/fund/inPay",//入金
        moneyOut:"/admin/fund/outPay",//出金
        withdrawal:"/admin/fund/withdraw",//提现
        charge:"/admin/fund/bankInfo"//充值
    };
    /**
     * 判断是否允许资金操作
     * @returns {boolean}
     * 未签约跟已申请解约
     */
    exports.checkAllowOperate=function () {
        var flag=true;
        common.ajaxHandle({
            url:urlJson.allow,
            async:false,
            callback:function(){},
            errorback:function(data) {
                flag=false;
                common.error(data.message);
            }
        });
        return flag;
    };
    /**
     * 获取附属账户余额
     * @returns {number}
     */
    exports.getVitrualAccount=function (isloading) {
        var sum=0.00;
        var isloading = isloading || true;
        common.ajaxHandle({
            url:urlJson.virtual,
            async:false,
            isLoading:isloading,
            callback:function (data) {
                if(data.data==null){
                    sum="0.00";
                }else {
                    sum = new Number(data.data).toFixed(2);
                }
            }
        });
        return sum;
    };
    /**
     * 支付密码校验
     * @param callback
     */
    exports.checkPayPwd = function(callback) {

        var html='<table class="table-form" style="margin-left:20px;margin-top:20px;"><input type="password" class="hidePwd"/>' +
            '<tr><th>交易密码</th><td><input type="password" name="pwd" class="form-control" autocomplete="off" maxlength="6"/> <a href="/member/member/safe" target="_blank" class="link2">忘记密码？</a></td></tr>' +
            '</table>';
        layer.open({
            type:1,
            title:"请输入交易密码",
            content:html,
            area:"400px",
            btn:["确定","取消"],
            yes:function(index,layero){
                var $pwd = layero.find("[name='pwd']");
                if($pwd.val()=="") {
                    layer.tips("请输入交易密码",$pwd);
                    return false;
                }
                common.ajaxHandle({
                    url:"/admin/fund/checkPayPassword",
                    data:{pwd:$pwd.val()},
                    closeLoading: false,
                    callback:function(item){
                        layer.close(index);
                        callback(item);
                    }
                });
            }
        });

    };
    /**
     * 入金
     */
    exports.inCash=function () {

        //是否允许资金操作检查
        if(!exports.checkAllowOperate()){
            return;
        }

        //附属账户余额
        var sum=exports.getVitrualAccount();
        var html='<table class="table-form">' +
            '<tr><th>附属账户余额</th><td class="c-primary">'+common.formatMoney(sum,2)+' 元</td></tr>' +
            '<tr><th>入金金额</th><td><input type="text" name="tPayAmount" class="form-control" autocomplete="off"/>元</td></tr>' +
            '</table>';
        var opt={
            type:2,
            emptyTipStr:"请输入大于0的金额",
            max:sum,
            maxTipStr:"您输入的金额不能大于" + sum
        };
        layer.open({
            title:"入金",
            type:1,
            content:html,
            area:"400px",
            btn:["确定","取消"],
            success:function ($box,index) {
                var $input=$box.find("input");
                common.unInputNumFormat($input,opt);
            },
            yes: function(index,$box){
                var $input=$box.find("input");
                var checkNumFormatInput=common.checkNumFormatInput($input,opt);
                if(!checkNumFormatInput){
                    return false;
                }
                layer.close(index);
                exports.checkPayPwd(function(item){
                    common.ajaxHandle({
                        url:urlJson.moneyIn,
                        data:{amount:$input.val(),token:item.data.token}
                    });
                })
            },
            end:function() {
                layer.closeAll('tips');
            }
        });
    };
    /**
     * 出金
     */
    exports.outCash=function () {
        //是否允许资金操作检查
        if(!exports.checkAllowOperate()){
            return;
        }
        //可用资金
        var sum=$("#js_useable").data("val");
        var opt={
            type:2,
            emptyTipStr:"请输入大于0的金额",
            max:sum,
            maxTipStr:"您输入的金额不能大于" + sum
        };
        var html='<table class="table-form" style="margin-left:20px;margin-top:20px;">' +
            '<tr><th>可用资金</th><td class="c-primary">'+common.formatMoney(sum,2)+' 元</td></tr>' +
            '<tr><th>出金金额</th><td><input type="text" name="tPayAmount" class="form-control" autocomplete="off"/>元</td></tr>' +
            '</table>';
        layer.open({
            title:"出金",
            type:1,
            content:html,
            area:"400px",
            btn:["确定","取消"],
            success:function ($box,index) {
                var $input=$box.find("input");
                common.unInputNumFormat($input,opt);
            },
            yes: function(index,$box){
                var $input=$box.find("input");
                var checkNumFormatInput=common.checkNumFormatInput($input,opt);
                if(!checkNumFormatInput){
                    return false;
                }
                layer.close(index);
                exports.checkPayPwd(function(item){
                    common.ajaxHandle({
                        url:urlJson.moneyOut,
                        data:{amount:$input.val(),token:item.data.token}
                    });
                });
            },
            end:function() {
                layer.closeAll('tips');
            }
        });
    };
    /**
     * 提现
     */
    exports.withdrawal=function () {
        //是否允许资金操作检查
        if(!exports.checkAllowOperate()){
            return;
        }
        //附属账户余额
        var sum=exports.getVitrualAccount();

        var html='<table class="table-form" style="margin-left:20px;margin-top:20px;">' +
            '<tr><th>附属账户余额</th><td class="c-primary">'+common.formatMoney(sum,2)+' 元</td></tr>' +
            '<tr><th>提现金额</th><td><input type="text" name="tPayAmount" class="form-control" autocomplete="off"/>元</td></tr>' +
            '</table>';
        var opt={
            type:2,
            emptyTipStr:"请输入大于0的金额",
            max:sum,
            maxTipStr:"您输入的金额不能大于" + sum
        };
        layer.open({
            title:"提现",
            type:1,
            content:html,
            area:"400px",
            btn:["确定","取消"],
            success:function ($box,index) {
                var $input=$box.find("input");
                common.unInputNumFormat($input,opt);
            },
            yes: function(index,$box){
                var $input=$box.find("input");
                var checkNumFormatInput=common.checkNumFormatInput($input,opt);
                if(!checkNumFormatInput){
                    return false;
                }
                layer.close(index);
                exports.checkPayPwd(function(item) {
                    common.ajaxHandle({
                        url: urlJson.withdrawal,
                        data: {amount: $input.val(),token:item.data.token}
                    });
                });
            },
            end:function() {
                layer.closeAll('tips');
            }
        });
    };

    // 新的充值函数，因为用window.open 很多浏览器会拦截窗口打开
    exports.getChargeSite = function() {
        var result={};
        common.ajaxHandle({
            url:urlJson.charge,
            async:false,
            isLoading: true,
            callback: function(item){
                result.bankType=item.data.bankType;
                result.bankId = item.data.bankId;
                result.virtualAccount = item.data.virtualAccount;
            }
        });
        if(result.bankType==2){
            //中信
            result.url = "http://bank.ecitic.com/";
            result.name = "中信银行";
            return result;
        }/*else if(result.bankType==2){
         //平安
         if(window.location.host.indexOf("dasu123.com")!=-1){
         result.url='https://ebank.sdb.com.cn/corporbank/index_payb.jsp';
         }else{
         result.url='https://testebank.sdb.com.cn/corporbank/index_payb.jsp';
         }
         result.name="平安银行";
         return result;
         }*/else if(result.bankType == 1 && result.bankId > 0){
            //var json =bankselector.getBankInfo(result.bankId);
            result.url= "http://www.cgbchina.com.cn/";
            result.name= "广发银行";
            return result;
        }
        //没银行签约
        result.url="javascript:;";
        return result;

    };

    //充值点击回调事件
    exports.chargeCallBack = function(siteJson) {
        var bankType = siteJson.bankType;
        var virtualAccount = siteJson.virtualAccount;
        var bank = siteJson.name;

        var html = '<div style="text-align: left;"><p style="font-size: 16px;color: #666;margin-bottom: 15px;">用网银或手机银行转账至您的银行存管账户</p>' +
            '<p><strong style="font-size: 22px;color: #ee802f; margin-right: 10px;" class="virtualAccount">'+virtualAccount+'</strong> </p>' +
            '<p style="margin-top: 5px;font-size: 12px;color: #999;line-height: 18px;">例：登录工商银行网上银行，在“转账汇款-境内汇款”，收款银行选择”'+bank+'“，转账至您的银行存管账户即可</p></div>';

        /*<a href="javascript:void(0);" id="copyVirtualAccount" style="font-size: 12px;vertical-align: top;color: #05c3f9;" data-clipboard-text="'+virtualAccount+'">复制</a>*/
        if(bankType==0){
            layer.alert('<p class="tc red">您的资金账户未签约绑定</p>',{title:"温馨提示"});
            return false;
        }else if(bankType==2) {
            //平安银行
            layer.alert('<p class="tc red">正在充值中……</p>',{title:"充值中"},function(){
                window.location.reload(true);
            });
            return false;
        }
       /* require('ZeroClipboard');*/
        layer.open({
            title: '温馨提示',
            content: html,
            area:"400px",
            btn:'确定',
            success:function(){
                //复制会员附属账户号
                /*var clip = new ZeroClipboard($("#copyVirtualAccount"));
                clip.on("copy", function(e){
                    layer.msg('<div>复制成功</div>');
                });*/
            }
        });


    };

    /**资金操作绑定*/
    exports.operate = function(bankType) {
        //入金
        $('[data-act="moneyIn"]').on("click",function () {
            exports.inCash();
            return false;
        });

        //出金
        $('[data-act="moneyOut"]').on("click",function () {
            exports.outCash();
            return false;
        });


        //提现
        $('[data-act="withdraw"]').on("click",function(){
            exports.withdrawal();
            return false;
        });

        //充值
        (function () {
            var site_json = exports.getChargeSite();
            var className = $("#chargeWrap").data("class");
            var a = '<a class="'+className+'" id="btnRecharge" href="javascript:void(0);" data-act="tooltipcol" data-text="从实体银行卡充值到代理商的银行存管账户"  style="cursor:pointer;">充值</a>';
            if(site_json.bankType==2) {
                a = '<a class="'+className+'" id="btnRecharge" target="_blank" href="'+site_json.url+'" data-act="tooltipcol" data-text="从实体银行卡充值到代理商的银行存管账户"  style="cursor:pointer;">充值</a>';
            }
            $("#chargeWrap").prepend(a);
            $("#btnRecharge").on("click", function () {
                exports.chargeCallBack(site_json);
                return false;
            });
        })();

    }
});
